package com.polaris.live.im.sfs.processor.impl

import com.lxj.xpopup.XPopup
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.dialog.LevelUpgradeDialog
import com.polaris.live.im.sfs.COMMAND_LEVEL_UP_MESSAGE
import com.polaris.live.im.sfs.bean.chat.UserRichCharmLevelUpgradeNoticeMessage
import com.polaris.live.im.sfs.bean.pusher.PusherNoticeMessage
import com.polaris.live.im.sfs.core.bean.DataArgument
import com.polaris.live.im.sfs.processor.AbstractMessageProcessor
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.ui.game.GameUtils
import com.polaris.live.ui.live.activity.StartMatchActivity
import kotlinx.coroutines.launch

/**
 * LevelUpMessageProcessor
 *
 * @author Created by 半仙 on 2024/1/30/030 15:26
 */
class LevelUpMessageProcessor : AbstractMessageProcessor() {

    override fun shouldProcess(type: Int, command: String): Boolean {
        return command == COMMAND_LEVEL_UP_MESSAGE
    }

    override suspend fun handle(type: Int, command: String, argument: DataArgument) {
        val data = argument.getData<PusherNoticeMessage>() ?: return
        val message =
            JsonUtils.fromJson<UserRichCharmLevelUpgradeNoticeMessage>(data.content) ?: return

        CoroutineScopeManager.mainScope.launch {
            val aliveTopActivity = ViewUtils.getAliveTopActivity() ?: return@launch
            if (!LiveStateManager.isNone() || aliveTopActivity is StartMatchActivity) return@launch
            if (GameUtils.gameRun) return@launch

            XPopup.Builder(aliveTopActivity)
                .isDestroyOnDismiss(true)
                .asCustom(LevelUpgradeDialog(aliveTopActivity, message))
                .show()
        }
    }
}